﻿var chkCounter = 0;

function loadGeneralSettings() {
    var parentDiv = $('#ucSettingsContainer');
    var moduleName = "../../_modules/settings/ucGeneral.ascx";
    var _params = '&z=';
    var opts = { 'onsucess': onUCGeneralLoad, 'params': _params };
    loadModuler(moduleName, parentDiv, opts);
}
function onUCGeneralLoad() {

}
function loadEmailSettings() {
    var parentDiv = $('#ucSettingsContainer');
    var moduleName = "../../_modules/settings/ucEmail.ascx";
    var _params = '&z=';
    var opts = { 'onsucess': onUCEmailLoad, 'params': _params };
    loadModuler(moduleName, parentDiv, opts);
}
function onUCEmailLoad() {

}
function loadProductSettings() {
    var parentDiv = $('#ucSettingsContainer');
    var moduleName = "../../_modules/settings/ucProducts.ascx";
    var _params = '&z=';
    var opts = { 'onsucess': onUCProductLoad, 'params': _params };
    loadModuler(moduleName, parentDiv, opts);
}
function onUCProductLoad() {
    fnLoadProdList();
}

function fnLoadProdList() {
    var parentDiv = $('#listExistingProd');
    var moduleName = "../../_modules/settings/ucListProducts.ascx";
    var _params = '&z=';
    var opts = { 'onsucess': '', 'params': _params };
    loadModuler(moduleName, parentDiv, opts);
}
function fnAddNewCategory() {
    var category = $('#txtCategory').val();
    if (category.length > 0) {
        //insertBrand.ashx
        var params = { 'nCate': category }
        var containerDIV = $('#addBrandLoadingGif');
        var opts = { 'isLoadingGif': true, 'container': containerDIV };
        MakeJAjaxCall("../../_services/insertCategory.ashx", params, 'html', fnAddCateSuccess, '', '', opts);
    }
    else {
        var msg = 'Please enter the category name';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': false };
        genAlertPOPUP(opts);
    }
}
function fnAddCateSuccess(response) {
    if (response == 'y') {
        loadProductSettings();
        var msg = 'Category added successfully';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': false };
        genAlertPOPUP(opts);
    }
    else {
        var msg = 'Error: Unable to save category name';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': false };
        genAlertPOPUP(opts);
    }
}
function fnGeneralClick(obj) {
    $('.StStng_Rght_ActClicked').removeClass('StStng_Rght_ActClicked').addClass('StStng_Rght_Act');
    $(obj).removeClass('StStng_Rght_Act').addClass('StStng_Rght_ActClicked');
    loadGeneralSettings();
}
function fnEmailClick(obj) {
    $('.StStng_Rght_ActClicked').removeClass('StStng_Rght_ActClicked').addClass('StStng_Rght_Act');
    $(obj).removeClass('StStng_Rght_Act').addClass('StStng_Rght_ActClicked');
    loadEmailSettings();
}
function fnProductClick(obj) {
    $('.StStng_Rght_ActClicked').removeClass('StStng_Rght_ActClicked').addClass('StStng_Rght_Act');
    $(obj).removeClass('StStng_Rght_Act').addClass('StStng_Rght_ActClicked');
    loadProductSettings();
}
function fnCheckIncoming(obj) {
    //alert("hi");
    if (chkCounter == 1) {
        $('#txtIncomingMail').removeAttr('disabled');
        $('#txtIncomingMail').val('');
        chkCounter = 0;
    }
    else {
        $('#txtIncomingMail').attr('disabled', 'disabled');
        $('#txtIncomingMail').val($('#txtOutgoingMail').val());
        chkCounter = 1;
    }
}

function fnAddNewSubCategory() {
    var cateid = $('#ctl02_ddlAddSubCate :selected').val();
    var subCate = $('#txtSubCate').val();
    if (cateid != 0 && subCate.length != 0) {
        var params = { 'cateid': cateid, 'subcate': subCate }
        var containerDIV = $('#addBrandLoadingGif');
        var opts = { 'isLoadingGif': true, 'container': containerDIV };
        MakeJAjaxCall("../../_services/insertSubCate.ashx", params, 'html', fnAddSubCateSuccess, '', '', opts);
    }
    else {
        $('#alertCateSelect').show();
        if (subCate.length == 0) {
            $('#sampleTextSpan').hide();
            $('#alertSpan').show();
        }
    }

}
function fnBindSubCategory() {
    $('#addModalAlert').hide();$('#addModalAlert').text('');
    var cateid = $('#ctl02_ddlProdCate').val();
    if (cateid != 0) {
        var params = { 'cateid': cateid}
        var containerDIV = $('#addBrandLoadingGif');
        var opts = { 'isLoadingGif': true, 'container': containerDIV };
        MakeJAjaxCall("../../_services/getSubCategory.ashx", params, 'html', fnGetSubCateSuccess, '', '', opts);
    }
}
function fnGetSubCateSuccess(response) {
    if (response != "n") {
        var data = $.parseJSON(response);
        if (data.length > 0) {
            $('#ddlDrodSubCate').removeAttr('disabled');
            for (var i = 0; i < data.length; i++) {
                var opt = $('<option></option>');
                $(opt).attr('value', data[i].id);
                $(opt).text(data[i].subcate);
                $('#ddlDrodSubCate').append(opt);
            }
        }
    }
    else {
        $('#ddlDrodSubCate').attr('disabled', 'disabled');
        $('#ddlDrodSubCate').empty();
    }

}
function fnAddSubCateSuccess(response) {
    if (response == "y") {
        loadProductSettings();
        var msg = 'Sub category added successfully';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': false };
        genAlertPOPUP(opts);
    }
    else {
        var msg = 'Error: Unable to save sub category name';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': false };
        genAlertPOPUP(opts);
    }
}
function fnAddNewBrand() {
    var brandName = $('#txtBrandName').val();
    //alert(brandName);
    if (brandName.length>0) {
        //insertBrand.ashx
        var params = { 'nBrand': brandName }
        var containerDIV = $('#addBrandLoadingGif');
        var opts = { 'isLoadingGif': true, 'container': containerDIV };
        MakeJAjaxCall("../../_services/insertBrand.ashx", params, 'html', fnAddBrandSuccess, '', '', opts);
    }
    else {
        var msg = 'Please enter the brand name';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': false };
        genAlertPOPUP(opts);
    }
}
function fnAddBrandSuccess(response) {
    if (response == 'y') {
        loadProductSettings();
        var msg = 'Brand name added successfully';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': false };
        genAlertPOPUP(opts);
    }
    else {
        var msg = 'Error: Unable to save brand name';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': false };
        genAlertPOPUP(opts);
    }
}
function fnAddModal() {
    var sel = $('select[id*=ddlBrand]');
    //alert($($(sel)+'option:selected').val());
    var brandId = $('#ctl02_ddlBrand option:selected').val();
    var brandName = $('#ctl02_ddlBrand option:selected').text();
    var productCate = $('#ctl02_ddlProdCate option:selected').val();
    var prodSubCateVal = $('#ddlDrodSubCate option:selected').val();
    var prodSubCateTxt = $('#ddlDrodSubCate option:selected').text();
    var prodStarVal = $('#ddlStarRating option:selected').val();
    var prodStarTxt = $('#ddlStarRating option:selected').text();
    var nModal = $('#txtModalName').val();
    var capacity = $('#txtProdCapacity').val();

    if (brandId == 0) {
        $('#addModalAlert').show();
        $('#addModalAlert').text("");
        $('#addModalAlert').text("** Please select the brand");
        return false;
    }
    if (productCate == 0) {
        $('#addModalAlert').show();
        $('#addModalAlert').text("");
        $('#addModalAlert').text("** Please select the category");
        return false;
    }
    if (capacity.length == 0) {
        $('#addModalAlert').show();
        $('#addModalAlert').text("");
        $('#addModalAlert').text("** Enter the product capacity");
        return false;
    }
    if (nModal.length == 0) {
        $('#addModalAlert').show();
        $('#addModalAlert').text("");
        $('#addModalAlert').text("** Enter the modal name");
        return false;
    }


    var fullProdName = "";

    if (prodSubCateVal != 0 && nModal.length > 0) {
        if (prodStarVal != 0) {
            fullProdName = brandName + " " + nModal + " " + prodSubCateTxt + " " + capacity + " ( " + prodStarTxt + " )";
            //alert(fullProdName);
        }
        else {
            fullProdName = brandName + " " + nModal + " " + prodSubCateTxt + " " + capacity;
            //alert(fullProdName);
        }
        var params = { 'brandId': brandId, 'nModal': nModal, 'fulProdNme': fullProdName,'prodcateid':productCate }
        var containerDIV = $('#addModalLoadingGif');
        var opts = { 'isLoadingGif': false, 'container': containerDIV };
        MakeJAjaxCall("../../_services/insertModal.ashx", params, 'html', fnAddModalSuccess, '', '', opts);
    }
    else {
        var msg = 'Please enter the modal name';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': false };
        genAlertPOPUP(opts);
    }

//    if (nModal.length > 0) {
//        var params = { 'brandId': brandId, 'nModal': nModal }
//        var containerDIV = $('#addModalLoadingGif');
//        var opts = { 'isLoadingGif': true, 'container': containerDIV };
//        MakeJAjaxCall("../../_services/insertModal.ashx", params, 'html', fnAddModalSuccess, '', '', opts);
//    }
//    else {
//        var msg = 'Please enter the modal name';
//        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': false };
//        genAlertPOPUP(opts);
//    }
}
function fnAddModalSuccess(response) {
    if (response == 'y') {
        $('#txtModalName').val('');
        var msg = 'Modal added successfully';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { fnLoadProdList(); $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': false };
        genAlertPOPUP(opts);
    }
    else {
        var msg = 'Error: Unable to add modal';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': false };
        genAlertPOPUP(opts);
    }
}
var isSSL = '';
var isClr = false;
function fnSaveEmailSettings() {
    var outgoingMail = $('#txtOutgoingMail').val();
    var IncomingMail = $('#txtIncomingMail').val();
    var mailServer = $('#txtMailServer').val();
    var portNo = $('#txtPortNo').val();
    isSSL = $('input:radio[name=ssl]:checked').val();
    if (outgoingMail == '') {
        $('#spnAlertText').text('** Please enter outgoing mail ID');
        isClr = false;
    }
    else if (IncomingMail == '') {
        $('#spnAlertText').text('** Please enter incoming mail ID');
        isClr = false;
    }
    else if (mailServer == '') {
        $('#spnAlertText').text('** Please enter mail server name');
        isClr = false;
    }
    else if (portNo == '') {
        $('#spnAlertText').text('** Please enter the port number');
        isClr = false;
    }
    else if ($('input:radio[name=ssl]').is(':checked') == false) {
        $('#spnAlertText').text('** Please enter if SSL required or not');
        isClr = false;
    }
    else if (validateEmail(outgoingMail) == false || validateEmail(IncomingMail) == false) {
        $('#spnAlertText').text('** Invalid email ID');
        isClr = false;
    }
    else {
        isClr = true;
    }
    if (isClr) {
        var params = { 'case': 'load', 'outmail': outgoingMail, 'inmail': IncomingMail, 'mailserv': mailServer, 'port': portNo, 'ssl': isSSL };
        var containerDIV = $('#addModalLoadingGif');
        var opts = { 'isLoadingGif': false, 'container': containerDIV };
        MakeJAjaxCall("../../_services/mailSettings.ashx", params, 'html', fnAddMailSettings, '', '', opts);
    }
}
function fnAddMailSettings(response) {

}
function fnClearAlert() {
    $('#spnAlertText').text('');
}
function validateEmail(txtEmail) {
    var a = txtEmail;
    var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
    if (filter.test(a)) {
        return true;
    }
    else {
        return false;
    }
}
function fnDeleteItem(obj) {
    var msg = 'Are you sure that you want to delete the product?';
    var opts = { 'AlertText': msg, 'OKTxt': 'Yes', 'CancelTxt': 'No', 'okClick': function () {
        var id = $(obj).attr('id');
        var params = { 'id': id };
        var containerDIV = $('#prodRptrContainer');
        var opts = { 'isLoadingGif': true, 'container': containerDIV };
        MakeJAjaxCall("../../_services/deleteSetProd.ashx", params, 'html', fnReloadContainer, '', '', opts);
    }, 'cancelClick': function () { $('body').find("#genricAlertPopup").remove(); }, 'reqCancel': true
    };
    genAlertPOPUP(opts);
}
function fnReloadContainer(response) {
    if (response != "n" || response != "l") {
        fnLoadProdList();
    }
}